package com.gykfbxxh.imovie.receiver;

import android.content.Context;
import android.os.Handler;
import android.text.TextUtils;

import com.app.utils.util.LogUtils;
import com.google.gson.Gson;
import com.gykfbxxh.imovie.AppApplication;
import com.gykfbxxh.imovie.bean.film.FilmEntity;
import com.gykfbxxh.imovie.config.AppConfigTools;
import com.gykfbxxh.imovie.config.ConfigKey;
import com.gykfbxxh.imovie.config.TypeConstant;
import com.gykfbxxh.imovie.manager.AppConfigManager;
import com.gykfbxxh.imovie.manager.IPushManager;
import com.umeng.message.UmengMessageHandler;
import com.umeng.message.entity.UMessage;

import java.util.Map;

/**
 * Created by Guo on 2016/8/9.
 * <p/>
 * 友盟推送的receiver
 */
public class UmengPushMessageReceiver extends UmengMessageHandler {

    /**
     * 参考集成文档的1.6.3
     * http://dev.umeng.com/push/android/integration#1_6_3
     */
    @Override
    public void dealWithCustomMessage(final Context context, final UMessage msg) {

        Map<String, String> extra = msg.extra;
        final String data = extra.get("data");
        LogUtils.e("gzq", "data= "+data);
        if (TextUtils.isEmpty(data)) return;
        Gson gson = new Gson();
        FilmEntity newsEntity = null;
        try {
            newsEntity = gson.fromJson(data, FilmEntity.class);
        } catch (Exception e) {
            e.printStackTrace();

        }
        final FilmEntity finalNewsEntity = newsEntity;
        new Handler().post(new Runnable() {
            @Override
            public void run() {
                if (finalNewsEntity != null) {
                    String showType = finalNewsEntity.getFFilmtype();
                    if (TextUtils.equals(showType, TypeConstant.PUSH_FOCUSNEWS)) {
                        //当前app运行并且在聚焦页面
//                        if (!IPushManager.getInstance().isAppIsInBackground(context)) {
//                            Activity lastActivity = AppManager.getAppManager().getLastActivity();
//                            if (lastActivity instanceof MainActivity) {
//                                boolean currentFragment = ((MainActivity) lastActivity).getCurrentFragment();
//                                LogUtils.e("gzq","c="+currentFragment);
////                                IPushManager.getInstance().showFoucsDialog(lastActivity, finalNewsEntity, TypeConstant.PUSHTYPE_UMENG);
//                            }
//                        } else {//记录到本地
                        AppConfigManager.getInstance().writeConfig(ConfigKey.push_foucusnews, data + "}_" + System.currentTimeMillis() + "}_" + TypeConstant.PUSHTYPE_UMENG);
                        AppConfigTools.isShowPushDialog = true;
//                        }


                    } else if (TextUtils.equals(showType, TypeConstant.PUSH_NORMAL)) {//不是要闻
                        String articleId = finalNewsEntity.getFId();
//                        if (!TextUtils.isEmpty(articleId) && UserDBDAO.ReceiverHistoryDB(articleId)) {
//                            return;
//                        }
                        IPushManager.getInstance().showNotification(AppApplication.getInstance(), finalNewsEntity, TypeConstant.PUSHTYPE_UMENG);
                    }
                }
            }
        });

    }

}
